﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;

namespace Smart.SignalRService
{
    public class CommonMethod
    {
        public static string ToJsonConvertExt(object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        public static T FromJsonTo<T>(string jsonString)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString.Replace("\0", ""))))
            {
                T jsonObject = (T)ser.ReadObject(ms);
                return jsonObject;
            }
        }

        #region 获取IP
        /// <summary>
        /// 获取IP
        /// </summary>
        /// <returns></returns>
        public static string GetIP()
        {
            string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(ip)) { ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; }
            if (string.IsNullOrEmpty(ip)) { ip = HttpContext.Current.Request.UserHostAddress; }
            if (string.IsNullOrEmpty(ip) || ip == "::1") { ip = "127.0.0.1"; }
            return ip;
        }
        #endregion
    }

    public class Common 
    {
        public static void WriteLog(string msg)
        {
            //depth所请求的堆栈帧的索引，在获得所调用的方法0，获得此方法的方法1，获得调用此方法的方法的方法2。。。类推
            //MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();
            try
            {
                StackTrace st = new StackTrace();
                string methodName = st.GetFrame(1).GetMethod().Name;
                string className = st.GetFrame(1).GetMethod().DeclaringType.ToString();

                FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + @"Log.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                m_streamWriter.WriteLine("");
                m_streamWriter.WriteLine("类名：" + className + "; 方法：" + methodName + "; 时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss[.fff] ") + " 日志：" + msg);
                m_streamWriter.Flush();
                m_streamWriter.Close();
                fs.Close();
            }
            catch
            {

            }
        }

        #region public static void WriteLogErr(string myErrEx) 系统错误日志
        /// <summary>
        /// 系统错误日志
        /// </summary>
        /// <param name="myEx"></param>
        public static void WriteLogErr(string myErrEx)
        {
            //depth所请求的堆栈帧的索引，在获得所调用的方法0，获得此方法的方法1，获得调用此方法的方法的方法2。。。类推
            //MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();
            try
            {
                StackTrace st = new StackTrace();
                string methodName = st.GetFrame(1).GetMethod().Name;
                string className = st.GetFrame(1).GetMethod().DeclaringType.ToString();

                FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + @"LogErr.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                m_streamWriter.WriteLine("");
                m_streamWriter.WriteLine("类名：" + className + "; 方法：" + methodName + "; 时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss[.fff] ") + " 错误信息：" + myErrEx);
                m_streamWriter.Flush();
                m_streamWriter.Close();
                fs.Close();
            }
            catch
            {

            }
        }
        #endregion

    }
}